// pages/login/login.js
import api from '../../utils/api.js'
let app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  handleGetUserInfo(e) {
    const self = this;
    console.log(e)
    if ('getUserInfo:ok' == e.detail.errMsg) {
      // app.globalData.userInfo = e.detail.userInfo;
      let iv = e.detail.iv;
      let encrypted_data = e.detail.encryptedData;
      wx.login({
        success: function(res) {
          console.log(res)
          if (res.code) {
            let code = res.code
            wx.request({
              url: api.API_URL + '/applet/customer/login',
              data: {
                code: code
              },
              method: 'POST',
              header: {
                'content-type': 'application/json'
              },
              success(res) {
                console.log(res);
                wx.setStorageSync('token', res.data.data.session_id);
                self.upData(iv, encrypted_data);
              }
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: e.detail.errMsg,
        icon: 'none',
      })
    }
  },
  upData: function (iv, encrypted) {
    const that = this;
    // api.POST('applet/customer/detail', {}, res => {
    //   console.log(res)
    //   app.globalData.userInfo = res.data.customer_wechat
    // })

    api.POST("/applet/customer/updateCustomerInfo", {
      encrypted_data: encrypted,
      iv: iv
    }, res => {
      wx.hideLoading();
      console.log(res, 'updata')
      api.POST('applet/customer/detail', {}, res => {
        console.log(res)
        app.globalData.userInfo = res.data.customer_wechat
      })
      wx.reLaunch({
        url: '../select/select'
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})